package com.example.administrator.myapplication.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;


import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.administrator.myapplication.R;
import com.example.administrator.myapplication.activity.NewModel;
import com.example.administrator.myapplication.activity.NewsActivity;
import com.example.administrator.myapplication.activity.VideoActivity;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.lang.reflect.Type;
import java.util.ArrayList;

public class ExercisesFragment extends Fragment {
    RecyclerView recyclerView;

    public ArrayList<NewModel> listNews = new ArrayList<>();




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_exercises, container, false);
        recyclerView = view.findViewById(R.id.news);
        loading();
        return view;
    }
    public void loading(){
        RequestParams params=new RequestParams("http://web.csmzxy.com/netCourse/readData");
        params.addQueryStringParameter("cmd","7");
        params.addQueryStringParameter("v1","274");
        params.addQueryStringParameter("v2","1");
        params.addQueryStringParameter("v3","30");
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Gson gson= new Gson();
                String result01=convertString(result);
                Type listType=new TypeToken<ArrayList<NewModel>>(){

                }.getType();
                if (gson==null){
                    gson=new Gson();
                }
                listNews  =gson.fromJson(result01,listType);
               GridLayoutManager glm = new GridLayoutManager(getActivity(), 1);
                recyclerView.setLayoutManager(glm);
                recyclerView.setAdapter(new MyAdapter());
                Log.i("xm", "result "+result01);
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }
    public class  MyViewHolder extends RecyclerView.ViewHolder{

        TextView textview;
        TextView timeview;


         public MyViewHolder(View itemView) {
            super(itemView);
             textview= itemView.findViewById(R.id.title_text);
             timeview= itemView.findViewById(R.id.time);

        }
    }

    public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {

        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = View.inflate(getActivity(), R.layout.item_news, null);
         MyViewHolder myViewHolder = new MyViewHolder(view);
            return myViewHolder;
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            final NewModel news=listNews.get(position);

            holder.textview.setText(news.contentTitle);
            holder.timeview.setText(news.submitTime);
            holder.textview.setOnClickListener(new View.OnClickListener() {


                @Override
                public void onClick(View view) {

                    Intent intent = new Intent(getActivity(), NewsActivity.class);
                    intent.putExtra("head", news.contentTitle);
                    intent.putExtra("body", news.contentID);
                    startActivity(intent);
                }
            });
        }


        @Override
        public int getItemCount() {
            return listNews.size();
        }
    }




    public String convertString(String result) {
        String result1 = result
                .replace(", responseSign : 00", "")
                .replace("\'", "\"")
                .replace("publishurl", "\"publishurl\"")
                .replace("webID", "\"webID\"")
                .replace("webname", "\"webname\"")
                .replace("webitemID", "\"webitemID\"")
                .replace("webitemname", "\"webitemname\"")
                .replace("websubitemname", "\"websubitemname\"")
                .replace("contentID", "\"contentID\"")
                .replace("contentAuthor", "\"contentAuthor\"")
                .replace("contentTitle", "\"contentTitle\"")
                .replace("submitTime", "\"submitTime\"")
                .replace("responseSign", "\"responseSign\"")
                .replace("websubitemID", "\"websubitemID\"");
        return result1;

    }



}
